package cn.stylefeng.guns.modular.business.frequency.orderarrive.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.orderarrive.entity.OrderArrive;
import cn.stylefeng.guns.modular.business.frequency.orderarrive.mapper.OrderArriveMapper;
import cn.stylefeng.guns.modular.business.frequency.orderarrive.model.param.OrderArriveParam;
import cn.stylefeng.guns.modular.business.frequency.orderarrive.service.OrderArriveService;
import cn.stylefeng.guns.modular.business.frequency.orderarrive.enums.OrderArriveExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单确认到达表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Service
public class OrderArriveServiceImpl extends ServiceImpl<OrderArriveMapper, OrderArrive> implements OrderArriveService {

    @Override
    public PageResult<OrderArrive> page(OrderArriveParam orderArriveParam) {

        // 构造条件
        LambdaQueryWrapper<OrderArrive> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderArrive> list(OrderArriveParam orderArriveParam) {

        // 构造条件
        LambdaQueryWrapper<OrderArrive> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderArriveParam orderArriveParam) {

        // 将dto转为实体
        OrderArrive orderArrive = new OrderArrive();
        BeanUtil.copyProperties(orderArriveParam, orderArrive);

        this.save(orderArrive);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderArriveParam orderArriveParam) {
        this.removeById(orderArriveParam.getId());
    }

    @Override
    public void edit(OrderArriveParam orderArriveParam) {

        // 根据id查询实体
        OrderArrive orderArrive = this.queryOrderArrive(orderArriveParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderArriveParam, orderArrive);

        this.updateById(orderArrive);
    }

    @Override
    public OrderArrive detail(OrderArriveParam orderArriveParam) {
        return this.queryOrderArrive(orderArriveParam);
    }



    /**
     * 获取订单确认到达表
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private OrderArrive queryOrderArrive(OrderArriveParam orderArriveParam) {
        OrderArrive orderArrive = this.getById(orderArriveParam.getId());
        if (ObjectUtil.isEmpty(orderArrive)) {
            throw new ServiceException(OrderArriveExceptionEnum.NOT_EXISTED);
        }
        return orderArrive;
    }
}